package com.sunb2b.service;

import java.util.List;

import javax.inject.Inject;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import com.sunb2b.dao.MemberDAO;
import com.sunb2b.entity.CompanyMember;
import com.sunb2b.entity.Member;
import com.sunb2b.entity.PersonMember;
import com.sunb2b.pojo.PageSearchResult;

@Service
public class MemberService {
    @Inject
    private MemberDAO memberDAO;

    public void saveMember(Member member) {
        memberDAO.save(member);
    }
    
    public Member getMember(int id){
        return memberDAO.findById(id);
    }
    
    public PageSearchResult search(DetachedCriteria dc, int page, int pageSize){
    	return memberDAO.search(dc, page, pageSize);
    }
    
    public List<Member> getMemberByName(String name){
    	return memberDAO.findByName(name);
    }
    
    public Member getMember(String username){
    	List list = memberDAO.findByUsername(username); 
    	return list==null||list.size()==0?null:(Member)list.get(0);
    }
    
    public Member checkUser(Member m){
    	List list = memberDAO.findByUsername(m.getUsername());
    	Member m1 = null;
    	if(list!=null&&list.size()!=0){
    		m1 = (Member)list.get(0);
    	}
    	else{
    		return null;
    	}
    	if(!m1.getPassword().equals(m.getPassword())){
    		return null;
    	}
    	return m1;
    }
    
    public List<Member> listAll(){
    	return memberDAO.findAll();
    }
    
    public List<PersonMember> listAllPersonMember(){
    	return memberDAO.findAllPersonMember();
    }
    
    public List<CompanyMember> listAllCompanyMember(){
    	return memberDAO.findAllCompanyMember();
    } 
    
}
